package com.smyh.model.host.param;

import android.content.Context;
import android.graphics.Color;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.smyh.UIM.host.param.GPRS_Param;
import com.smyh.bean.GprsParamInfo;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.model.tableModel.KtableView;
import com.smyh.protcol.host.HostCMDUtil;
import com.smyh.util.HexStrUtil;
import com.smyh.util.StringFormat;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by 姜龙 on 2016/7/8.
 */
public class GPRSModel {

    private static TableLayout tableLayout;

    public static TableLayout getTableLayout(Context context){
        if(tableLayout == null){
            tableLayout = new TableLayout(context);
            tableLayout.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(1);
            String header0[] = {"GPRS参数"};
            String header1[] = {"名称","值","描述"};
            String test1[] = {"主IP地址","","4字节"};
            String test2[] = {"主端口号","","2字节"};
            String test3[] = {"备用IP地址","","4字节"};
            String test4[] = {"备用端口号","","2字节"};
            String test5[] = {"APN接入点","","20字节"};
            String test6[] = {"主卡号码","","11字节"};
            String test7[] = {"副卡号码","","11字节"};
            tableLayout.addView(KtableView.addTableHeaders(context,header0));
            tableLayout.addView(KtableView.addTableHeaders(context,header1));
            tableLayout.addView(KtableView.addNewRow(context,test1));
            tableLayout.addView(KtableView.addNewRow(context,test2));
            tableLayout.addView(KtableView.addNewRow(context,test3));
            tableLayout.addView(KtableView.addNewRow(context,test4));
            tableLayout.addView(KtableView.addNewRow(context,test5));
            tableLayout.addView(KtableView.addNewRow(context,test6));
            tableLayout.addView(KtableView.addNewRow(context,test7));
        }
        return tableLayout;
    }

    public static IoBuffer writeParam(){
        //硬件参数修改
        IoBuffer buf = IoBuffer.allocate(0).setAutoExpand(true);
        for (int j = 2; j < tableLayout.getChildCount(); j++) {
            TableRow childRow = (TableRow) tableLayout.getChildAt(j);
                switch (j) {
                    case 2: {
                        //192.168.1.1
                        String s = ""+ (((TextView) childRow.getChildAt(1)).getText().toString().equals("")?"0.0.0.0":((TextView) childRow.getChildAt(1)).getText().toString());
                        buf.put(StringFormat.ipToBytes(s));
                        break;
                    }
                    case 3: {

                        int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                        byte[] bTemp =  HexStrUtil.hexStr2Bytes(String.format("%04X",cell));
                        byte[] bt = new byte[2];
                        for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                            bt[i] = bTemp[i];
                        }
                        buf.put(bt);
                        break;
                    }
                    case 4: {
                        //192.168.1.2
                        String s = ""+ (((TextView) childRow.getChildAt(1)).getText().toString().equals("")?"0.0.0.0":((TextView) childRow.getChildAt(1)).getText().toString());
                        buf.put(StringFormat.ipToBytes(s));
                        break;
                    }
                    case 5: {
                        int cell = Integer.valueOf(""+(((TextView) childRow.getChildAt(1)).getText().toString().equals("")?0:((TextView) childRow.getChildAt(1)).getText().toString())) ;
                        byte[] bTemp =  HexStrUtil.hexStr2Bytes(String.format("%04X",cell));
                        byte[] bt = new byte[2];
                        for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                            bt[i] = bTemp[i];
                        }
                        buf.put(bTemp);
                        break;
                    }
                    case 6: {
                        String s = ((TextView) childRow.getChildAt(1)).getText().toString();
                        String replaceAll = StringFormat.stringToHex(s).replaceAll(" ", "");
                        byte[] bTemp = HexStrUtil.hexStr2Bytes(replaceAll);
                        // APN接入点名称
                        byte[] bt = new byte[20];
                        for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                            bt[i] = bTemp[i];
                        }
                        if(bTemp.length<20){
                            bt[bTemp.length] = (byte) 0xff;
                        }
                        buf.put(bt);
                        break;
                    }
                    case 7: {
                        String s = ((TextView) childRow.getChildAt(1)).getText().toString();
                        String replaceAll = StringFormat.stringToHex(s).replaceAll(" ", "");
                        byte[] bTemp = HexStrUtil.hexStr2Bytes(replaceAll);
                        // 主卡号-11字节
                        byte[] bt = new byte[11];
                        for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                            bt[i] = bTemp[i];
                        }
                        buf.put(bt);
                        break;
                    }
                    case 8: {
                        String s = ((TextView) childRow.getChildAt(1)).getText().toString();
                        String replaceAll = StringFormat.stringToHex(s).replaceAll(" ", "");
                        byte[] bTemp = HexStrUtil.hexStr2Bytes(replaceAll);
                        // 副卡号-11字节
                        byte[] bt = new byte[11];
                        for (int i = 0; i < bTemp.length && i < bt.length; i++) {
                            bt[i] = bTemp[i];
                        }
                        buf.put(bt);
                        break;
                    }
                }
            }
            byte[] bTemp = new byte[26];
            buf.put(bTemp);
            buf.flip();
            IoBuffer buf1 = HostCMDUtil.writeParam(3, buf,(byte)0);
            return buf1;
    }
    public static void setTableLayout() {

        for (int i = 2; i < tableLayout.getChildCount(); i++) {
            TableRow childRow = (TableRow) tableLayout.getChildAt(i);
            GprsParamInfo info = GPRS_Param.gprsParamInfoMap.get(MainCahce.chooseDeviceAddr);
            if(info != null){

                switch (i) {
                    case 2:
                        ((TextView) childRow.getChildAt(1)).setText(StringFormat.stringToIp(info.getIpA()) + "");
                        break;
                    case 3:
                        ((TextView) childRow.getChildAt(1)).setText(info.getPortA() + "");
                        break;
                    case 4:
                        ((TextView) childRow.getChildAt(1)).setText(StringFormat.stringToIp(info.getIpB()) + "");
                        break;
                    case 5:
                        ((TextView) childRow.getChildAt(1)).setText(info.getPortB() + "");
                        break;
                    case 6:
                        String apnStr = info.getAPN();
                        apnStr.toUpperCase();
                        apnStr = apnStr.substring(0, apnStr.indexOf("FF"));
                        ((TextView) childRow.getChildAt(1)).setText(StringFormat.hexToString(apnStr) + "");
                        break;
                    case 7:
                        ((TextView) childRow.getChildAt(1)).setText(StringFormat.hexToString(info.getTelNum()) + "");
                        break;
                    case 8:
                        ((TextView) childRow.getChildAt(1)).setText(StringFormat.hexToString(info.getTelNum2()) + "");
                        break;


                }
            }else {

                switch (i) {
                    case 2:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 3:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 4:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 5:
                        ((TextView) childRow.getChildAt(1)).setText(" ");
                        break;
                    case 6:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;
                    case 7:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;
                    case 8:
                        ((TextView) childRow.getChildAt(1)).setText( " ");
                        break;


                }
            }
        }
    }
}
